package com.zbcz.crm

import InvitedBeanListFragment.Companion.createInvitedBeanListFragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.zbcz.crm.adapter.ProjectBeanListAdapter.Companion.PROJECT_BEAN
import com.zbcz.crm.bean.ProjectBean
import com.zbcz.crm.databinding.ActivityMarketManagerBinding
import com.zbcz.crm.fragment.marketmanager.AcceptanceFragment.Companion.createAcceptanceFragment
import com.zbcz.crm.fragment.marketmanager.AuditBeanFragment.Companion.createAuditFragment
import com.zbcz.crm.fragment.marketmanager.ContractViewFragment.Companion.createContractFragment
import com.zbcz.crm.fragment.marketmanager.FollowUpRecordFragment.Companion.createFollowUpFragment
import com.zbcz.crm.fragment.marketmanager.PaymentBackFragment.Companion.createPaymentBackFragment
import com.zbcz.crm.fragment.marketmanager.ProjectEstablishmentFragment.Companion.createProjectEstablishFragment
import com.zbcz.crm.fragment.marketmanager.ProjectFileFragment.Companion.createProjectFileFragment
import com.zbcz.crm.fragment.marketmanager.TenderFragment.Companion.createTenderListFragment
import com.zbcz.crm.fragment.marketmanager.WinningBidFragment.Companion.createWinningBidListFragment
import com.zbcz.crm.util.onSingleClick

class MarketManagerActivity : BaseActivity() {

    private lateinit var mBinding: ActivityMarketManagerBinding

    companion object {
        val mList =
            listOf(
                "跟进记录",
                "立项",
                "招标",
                "投标",
                "中标通知书",
                "合同",
                "验收",
                "审计",
                "回款",
                "归档资料"
            )

        const val PROJECT_ID = "PROJECT_ID"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMarketManagerBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        init()
    }

    private var mProjectBean: ProjectBean? = null

    fun init() {
        mBinding.ivBack.onSingleClick {
            finish()
        }
        mProjectBean = intent?.getParcelableExtra(PROJECT_BEAN)
        initViewPage2()
    }

    class MarketManagerFragmentAdapter(
        fragmentManager: FragmentManager,
        lifecycle: Lifecycle
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {

        private val fragmentList = mutableListOf<Fragment>()

        fun addFragment(fragment: Fragment) {
            fragmentList.add(fragment)
        }

        override fun getItemCount(): Int {
            return fragmentList.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragmentList[position]
        }
    }

    private fun initViewPage2() = with(mBinding) {
        val adapter = MarketManagerFragmentAdapter(supportFragmentManager, lifecycle).apply {
//            addFragment(FileListFragment.createInstance("接收", 0, "1"))
            addFragment(createFollowUpFragment(mProjectBean?.id ?: ""))
            addFragment(createProjectEstablishFragment(mProjectBean?.id ?: ""))
            addFragment(createInvitedBeanListFragment(mProjectBean?.id ?: ""))
            addFragment(createTenderListFragment(mProjectBean?.id ?: ""))
            addFragment(createWinningBidListFragment(mProjectBean?.id ?: ""))
            addFragment(createContractFragment(mProjectBean?.id ?: ""))
            addFragment(createAcceptanceFragment(mProjectBean?.id ?: ""))
            addFragment(createAuditFragment(mProjectBean?.id ?: ""))
            addFragment(createPaymentBackFragment(mProjectBean?.id ?: ""))
            addFragment(createProjectFileFragment(mProjectBean?.id ?: ""))
        }
        viewPager2.adapter = adapter
        TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
//            tab.text = "Tab ${position + 1}" // 设置选项卡标题，可以根据实际情况修改
            tab.text = mList[position]
        }.attach()
    }
}